{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "***** xtdata连接成功 *****\n",
      "服务信息: None\n",
      "服务地址: None\n",
      "数据路径: None\n",
      "设置xtdata.enable_hello = False可隐藏此消息\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'timetag': '20240409 13:20:34',\n",
       " 'lastPrice': 1661.84,\n",
       " 'open': 1660.67,\n",
       " 'high': 1673,\n",
       " 'low': 1658.45,\n",
       " 'lastClose': 1666.66,\n",
       " 'amount': 2880890500,\n",
       " 'volume': 17318,\n",
       " 'pvolume': 1731832,\n",
       " 'stockStatus': 0,\n",
       " 'openInt': 13,\n",
       " 'settlementPrice': 0,\n",
       " 'lastSettlementPrice': 0,\n",
       " 'askPrice': [1661.99, 1662, 1663, 0, 0],\n",
       " 'bidPrice': [1661.1, 1660.88, 1660.86, 0, 0],\n",
       " 'askVol': [4, 14, 2, 0, 0],\n",
       " 'bidVol': [1, 3, 3, 0, 0]}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "res = xtdata.get_full_tick(['600519.SH'])\n",
    "\n",
    "res['600519.SH']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "***** xtdata连接成功 *****\n",
      "服务信息: None\n",
      "服务地址: None\n",
      "数据路径: None\n",
      "设置xtdata.enable_hello = False可隐藏此消息\n",
      "\n",
      "{'600519.SH': {'time': 1712640163000, 'lastPrice': 1661.65, 'open': 1660.67, 'high': 1673.0, 'low': 1658.45, 'lastClose': 1666.66, 'amount': 2894684200.0, 'volume': 17401, 'pvolume': 1740132, 'stockStatus': 0, 'openInt': 13, 'transactionNum': 0, 'lastSettlementPrice': 0.0, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [1662.24, 1662.68, 1662.8500000000001, 0.0, 0.0], 'bidPrice': [1661.64, 1661.6100000000001, 1661.6000000000001, 0.0, 0.0], 'askVol': [1, 3, 1, 0, 0], 'bidVol': [1, 2, 2, 0, 0], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}}\n",
      "{'600519.SH': {'time': 1712640166000, 'lastPrice': 1662.54, 'open': 1660.67, 'high': 1673.0, 'low': 1658.45, 'lastClose': 1666.66, 'amount': 2895016700.0, 'volume': 17403, 'pvolume': 1740332, 'stockStatus': 0, 'openInt': 13, 'transactionNum': 0, 'lastSettlementPrice': 0.0, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [1662.24, 1662.8500000000001, 1662.99, 0.0, 0.0], 'bidPrice': [1661.64, 1661.6100000000001, 1661.6000000000001, 0.0, 0.0], 'askVol': [2, 1, 1, 0, 0], 'bidVol': [1, 2, 2, 0, 0], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}}\n",
      "{'600519.SH': {'time': 1712640169000, 'lastPrice': 1661.66, 'open': 1660.67, 'high': 1673.0, 'low': 1658.45, 'lastClose': 1666.66, 'amount': 2895515200.0, 'volume': 17406, 'pvolume': 1740632, 'stockStatus': 0, 'openInt': 13, 'transactionNum': 0, 'lastSettlementPrice': 0.0, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [1662.0, 1662.24, 1662.8500000000001, 0.0, 0.0], 'bidPrice': [1661.6100000000001, 1661.6000000000001, 1661.52, 0.0, 0.0], 'askVol': [1, 1, 1, 0, 0], 'bidVol': [2, 2, 1, 0, 0], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}}\n",
      "{'600519.SH': {'time': 1712640172000, 'lastPrice': 1661.6100000000001, 'open': 1660.67, 'high': 1673.0, 'low': 1658.45, 'lastClose': 1666.66, 'amount': 2895847600.0, 'volume': 17408, 'pvolume': 1740832, 'stockStatus': 0, 'openInt': 13, 'transactionNum': 0, 'lastSettlementPrice': 0.0, 'settlementPrice': 0.0, 'pe': 0.0, 'askPrice': [1662.24, 1662.8500000000001, 1662.99, 0.0, 0.0], 'bidPrice': [1661.6100000000001, 1661.6000000000001, 1661.52, 0.0, 0.0], 'askVol': [1, 1, 1, 0, 0], 'bidVol': [2, 2, 1, 0, 0], 'volRatio': 0.0, 'speed1Min': 0.0, 'speed5Min': 0.0}}\n"
     ]
    }
   ],
   "source": [
    "from xtquant import xtdata\n",
    "\n",
    "def on_data (datas):\n",
    "    print(datas)\n",
    "\n",
    "seq = xtdata.subscribe_whole_quote(code_list=['600519.SH'], callback=on_data)\n",
    "\n",
    "xtdata.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "from datetime import datetime\n",
    "import time\n",
    "\n",
    "def on_data (datas):\n",
    "    tick_time = datas['600519.SH']['time']\n",
    "    timestamp_seconds = tick_time / 1000\n",
    "    readable_time = datetime.fromtimestamp(timestamp_seconds).strftime('%Y-%m-%d %H:%M:%S.%f')\n",
    "    # 获取当前时间戳（秒级）\n",
    "    current_timestamp_seconds = time.time()\n",
    "    current_readable_time = datetime.fromtimestamp(current_timestamp_seconds).strftime('%Y-%m-%d %H:%M:%S.%f')\n",
    "    print(readable_time)\n",
    "    print(current_readable_time)\n",
    "    print(datas)\n",
    "\n",
    "seq = xtdata.subscribe_whole_quote(code_list=['600519.SH'], callback=on_data)\n",
    "\n",
    "xtdata.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "from datetime import datetime\n",
    "import time\n",
    "\n",
    "def on_data (datas):\n",
    "    tick_time = datas['600519.SH'][0]['time']\n",
    "    timestamp_seconds = tick_time / 1000\n",
    "    readable_time = datetime.fromtimestamp(timestamp_seconds).strftime('%Y-%m-%d %H:%M:%S.%f')\n",
    "    # 获取当前时间戳（秒级）\n",
    "    current_timestamp_seconds = time.time()\n",
    "    current_readable_time = datetime.fromtimestamp(current_timestamp_seconds).strftime('%Y-%m-%d %H:%M:%S.%f')\n",
    "    print(readable_time)\n",
    "    print(current_readable_time)\n",
    "    print(datas)\n",
    "\n",
    "seq = xtdata.subscribe_quote(stock_code='600519.SH', period='1m', callback=on_data)\n",
    "\n",
    "xtdata.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xtquant import xtdata\n",
    "import time\n",
    "\n",
    "def on_data (datas):\n",
    "    print(datas)\n",
    "\n",
    "seq = xtdata.subscribe_whole_quote(code_list=['600519.SH'], callback=on_data)\n",
    "\n",
    "time.sleep(10)\n",
    "\n",
    "xtdata.unsubscribe_quote(seq)\n",
    "\n",
    "print('取消订阅成功')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "miniqmt-demo-cptEsqYo-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
